package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/9/18 0018
 * Description: 顶部切换栏，固定包含两项
 */

public class TopSwitcher extends FrameLayout implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;

    public TopSwitcher(@NonNull Context context) {
        this(context, null);
    }


    private static final int LEFT_TAG = 0;
    private static final int RIGHT_TAG = 1;
    private TextView mTxtLeft;
    private TextView mTxtRight;

    public TopSwitcher(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_top_switcher, this);
        mTxtLeft = view.findViewById(R.id.txt_left);
        mTxtRight = view.findViewById(R.id.txt_right);
        mTxtLeft.setTag(LEFT_TAG);
        mTxtRight.setTag(RIGHT_TAG);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopSwitcher);
        String txtLeft = array.getString(R.styleable.TopSwitcher_txt_left);
        String txtRight = array.getString(R.styleable.TopSwitcher_txt_right);
        array.recycle();


        mTxtLeft.setText(txtLeft);
        mTxtRight.setText(txtRight);
        mTxtLeft.setOnClickListener(this);
        mTxtRight.setOnClickListener(this);
    }

    public void setupViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        mTxtLeft.setSelected(true);
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mViewPager == null) {
            return;
        }
        int tag = Integer.parseInt(String.valueOf(v.getTag()));
        switch (tag) {
            case LEFT_TAG:

                if (mTxtLeft.isSelected()) {
                    return;
                }
                mTxtLeft.setSelected(true);
                mTxtRight.setSelected(false);
                mViewPager.setCurrentItem(tag);
                break;
            case RIGHT_TAG:
                if (mTxtRight.isSelected()) {
                    return;
                }
                mTxtLeft.setSelected(false);
                mTxtRight.setSelected(true);
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mTxtLeft.setSelected(true);
            mTxtRight.setSelected(false);
        } else {
            mTxtLeft.setSelected(false);
            mTxtRight.setSelected(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
